﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;


namespace MyMovieSystem
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 存储窗体坐标
        /// </summary>
        public Point p = new Point();

        #region 加载事件
        //窗体加载
        private void FrmLogin_Load(object sender, EventArgs e)
        {
            //初始化影片
            //MovieManage.InitialMovie();
            //MovieManage.SerializeMovie();
            //初始化角色
            RoleManage.InitialRole();
            //初始化已购买的票
           // TicketManage.InitialTicket();

            if (p.X == 0 && p.Y == 0)
                return;
            this.Location = p;
        }
        #endregion

        #region 登录
        //登录
        private void btnLogin_Click_1(object sender, EventArgs e)
        {
            string name = txtUserName.Text.Trim();
            string pwd = txtPwd.Text.Trim();
            if (CheckLogin(name, pwd))
            {
                //判断登录角色为用户
                if (RoleManage.dirRole[name] is User)
                {
                    FrmAllM  all = new FrmAllM();
                    User u = (User)RoleManage.dirRole[name];
                    u.WantMovie = new List<Movie>();
                    u.LookOffMovie = new List<Movie>();
                    RoleManage.user =u;
                    RoleManage.admin = null;
                    all.Show();
                    this.Hide();
                }
                //否则为管理员
                else
                {
                    FrmMain main = new FrmMain();
                    RoleManage.user = null;
                    RoleManage.admin = (Admin)RoleManage.dirRole[name];
                    main.Show();
                    this.Hide();
                }
                //登录成功后将数据序列化至本地
                RoleManage.SerializeRole();
            }
            
        }
        #endregion

        #region 登录验证
        /// <summary>
        /// 登录验证
        /// </summary>
        private bool CheckLogin(string name,string pwd)
        {
            if (txtUserName.Text.Trim().Length < 1 || txtPwd.Text.Trim().Length < 1)
            {
                label4.Visible = true;
                return false;
            }
            try
            {
                if (RoleManage.dirRole[name].Pwd != pwd)
                {
                    MessageBox.Show("用户名和密码不匹配！", "友情提示：");
                    return false;
                }
            }
            catch
            {
                MessageBox.Show("用户名不存在！", "友情提示：");
                return false ;
            }
            return true;
        }
        #endregion

        #region 注册
        //注册
        private void btnSignIn_Click(object sender, EventArgs e)
        {
            FrmSignIn signIn = new FrmSignIn();
            signIn.p = this.Location;
            signIn.Show();
            this.Hide();
        }
        #endregion

        #region textbox提示文本
        //textbox文本值更改时发生
        private void TextChangeds(object sender, EventArgs e)
        {
            label4.Visible = false;
            if (sender.Equals(txtUserName))
            {
                label2.Visible = txtUserName.Text.Length < 1;
            }
            else if (sender.Equals(txtPwd))
            {
                label3.Visible = txtPwd.Text.Length < 1;
            }
        }

        //单击label控件发生
        private void label2_Click(object sender, EventArgs e)
        {
            if (sender.Equals(label2))
            {
                txtUserName.Focus();
            }
            else if (sender.Equals(label3))
            {
                txtPwd.Focus();
            }
        }
        #endregion

        //跳过登录
        private void label1_Click(object sender, EventArgs e)
        {
            FrmAllM all = new FrmAllM();
            RoleManage.user = null;
            RoleManage.admin = null;
            all.Show();
            this.Hide();
        }

        private void lblForgetPwd_Click(object sender, EventArgs e)
        {

        }
    }
}
